package com.weiaibenpao.demo.chislim.retrofit.rx;

import com.orhanobut.logger.Logger;
import com.weiaibenpao.demo.chislim.retrofit.NetWorkManager;
import com.weiaibenpao.demo.chislim.retrofit.model.BaseResponse;

import java.util.concurrent.TimeUnit;

import io.reactivex.Observable;
import io.reactivex.ObservableSource;
import io.reactivex.ObservableTransformer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Function;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by wlx on 2017/12/5.
 */

/**
 * 网络请求通用设置转换器
 * 组件化的i情况下，如果解析失败，请使用JsonArrayParesTransformer,JsonParesTransformer
 * @param <T>
 */
public class NetWorkTransformer<T> implements ObservableTransformer<BaseResponse<T>,T> {

    private static final int DEFAULT_TIME_OUT = 5;
    private static final int DEFAULT_RETRY = 5;

    /**
     * 处理请求结果，BaseResponse
     * @param response 请求结果
     * @return 过滤处理，返回只有data的Observable
     */
    private Observable<T> flatResponse(final  BaseResponse<T> response){
        return Observable.just(response).
                map(new Function<BaseResponse<T>, T>() {
                    @Override
                    public T apply(BaseResponse<T> response) throws Exception {
                        //拿到后台返回的 code
                        int code = response.getCode();
                        //通过code获取注册的接口回调
                        APIExceptionCallBack apiCallback = NetWorkManager.getApiCallback(code);
                        //如果请求成功。直接返回数据
                        if (apiCallback== APISuccessCallback.INSTANCE){
                            return response.getData();
                        }
                        //code为错误码，获取全局统一处理
                        APIExceptionCallBack commonApiCallback = NetWorkManager.getApiCallback(NetWorkManager.API_COMMON_EXCEPTION_CODE);
                        String errorMsg=response.getMessage();
                        //如果配置全局，走全局处理
                        if (commonApiCallback!=null){
                            Logger.e("commonApiCallback");
                            errorMsg= commonApiCallback.callBack(response);
                        }else if (apiCallback!=null){//否则走单个注册处理
                            Logger.e("apiCallback");

                            errorMsg=apiCallback.callBack(response);
                        }
                        //如果开启Apixception，抛出服务器异常
                        if (NetWorkManager.isOpenApiException()){
                            errorMsg=response.getMessage();
                        }
                        //抛出异常，走onError
                        throw new APIException(code,errorMsg);
                    }
                });
    }




    @Override
    public ObservableSource<T> apply(Observable<BaseResponse<T>> upstream) {
        return upstream.
                subscribeOn(Schedulers.io()).
                observeOn(AndroidSchedulers.mainThread()).
                unsubscribeOn(Schedulers.io()).
                timeout(DEFAULT_TIME_OUT, TimeUnit.SECONDS).
                retry(DEFAULT_RETRY).
                flatMap(new Function<BaseResponse<T>, ObservableSource<T>>() {
            @Override
            public ObservableSource<T> apply(BaseResponse<T> response) throws Exception {
                return flatResponse(response);
            }
        });
    }



}
